package com.travel.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.travel.entity.Comment;
import com.travel.service.HotelCommentService;
import com.travel.service.impl.HotelCommentServiceImpl;
import com.travel.util.PageInfo;

/**
 * Servlet implementation class HotelCommentServlet
 */
@WebServlet("/HotelCommentServlet")
public class HotelCommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private HotelCommentService hotelCommentService = new HotelCommentServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HotelCommentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op = request.getParameter("op");
		if (op==null) {
			// 查询列表
			doFindHotelComment(request, response);
		} else if("delete".equals(op)){
			doDeleteHotelComment(request, response);
		} else if("searchByPage".equals(op)) {
			// 分页查询列表
			doQueryCommentByPage(request, response);
		} else if("publishComment".equals(op)) {
			// 分页查询列表
			doPublishComment(request, response);
			doFindHotelComment(request, response);
		} 
		
	}

	private void doPublishComment(HttpServletRequest request, HttpServletResponse response) {
		
		String commentContent = request.getParameter("commentContent");
		String commentDatetime = request.getParameter("commentDatetime");
		int userId = Integer.parseInt(request.getParameter("userId"));
		int hotelId = Integer.parseInt(request.getParameter("hotelId"));
		Comment comment = new Comment(null, commentContent, commentDatetime, userId, hotelId, null, null, null);
		hotelCommentService.insert(comment);
		
		
	}

	private void doQueryCommentByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		PageInfo<Comment> pageInfo=hotelCommentService.getUserListByPage(pageNum, pageSize);
		// 封装数据
	    ObjectMapper objectMapper = new ObjectMapper();
	    String json = objectMapper.writeValueAsString(pageInfo);
	    // 设置响应内容类型
	    response.setContentType("application/json;charset=utf-8");
	    // 响应数据
	    PrintWriter  out= response.getWriter();
	    out.print(json);		
	}

	private void doDeleteHotelComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String commentId=request.getParameter("commentId");
		hotelCommentService.deleteById(Integer.parseInt(commentId));
		int pageNum = Integer.parseInt(request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		PageInfo<Comment> pageInfo=hotelCommentService.getUserListByPage(pageNum, pageSize);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(pageInfo);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
	}

	private void doFindHotelComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		System.out.println("执行了doFindHotelComment方法");
		List<Comment> list = hotelCommentService.getCommentList();
		System.out.println(list);
		// 封装数据
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		// 设置响应内容类型
		response.setContentType("application/json;charset=utf-8");
		// 响应数据
		PrintWriter out = response.getWriter();
		out.print(json);
		System.out.println(json);
		
	}

}







